package slr.comment.portlet.data;

import java.util.List;

import slr.comment.model.SlrAnchor;
import slr.comment.model.SlrComment;
import slr.comment.portlet.ui.SLRCommentsTable;
import slr.comment.portlet.util.Lang;
import slr.comment.portlet.util.SLRCommentsUtil;
import slr.comment.service.SlrCommentLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

/**
 * The right hand side of the Explorer is diplayed with the Tabsheet.
 * 
 * @author TransIT mPower Labs
 * 
 */
public class SlrExplorerContainer {
	private static final long serialVersionUID = -3803643550151923315L;

	public final VerticalLayout firstLayout = new VerticalLayout();
	public final VerticalLayout secondLayout = new VerticalLayout();
	public final VerticalLayout thirdLayout = new VerticalLayout();
	public final VerticalLayout fourthLayout = new VerticalLayout();

	public TabSheet tabSheet;
	public static String listName = "";
	public long slrDocId;

	public SlrExplorerContainer() {
		tabSheet = new TabSheet();
		tabSheet.setHeight(100, TabSheet.UNITS_PERCENTAGE);
		tabSheet.setWidth(100, TabSheet.UNITS_PERCENTAGE);
		secondLayout.setHeight(100, TabSheet.UNITS_PERCENTAGE);
		thirdLayout.setHeight(100, TabSheet.UNITS_PERCENTAGE);

	}

	/**
	 * The Tabs are added corresponding to the Node selected from the
	 * Hierarchial Tree.
	 * 
	 * @param object
	 * @return Tabsheet with coreeponding component added to the Tabsheet
	 */
	public TabSheet addTabSheets(Object object) {
		final SLRDetailsContent addDynComponent = new SLRDetailsContent();

		firstLayout.removeAllComponents();
		secondLayout.removeAllComponents();
		thirdLayout.removeAllComponents();
		fourthLayout.removeAllComponents();

		tabSheet.removeAllComponents();

		firstLayout.addComponent(addDynComponent.addDetailComponent(object));

		if (object instanceof SlrAnchor) {
			final SlrAnchor slrAnchor = (SlrAnchor) object;
			slrDocId = SLRCommentsUtil.getSlrDocId(slrAnchor);

			List<SlrComment> slrCommentList = null;
			SLRCommentsTable commentsTable = null;
			try {
				slrCommentList = SlrCommentLocalServiceUtil
						.findByCommentTypAndAnchorId("F", slrAnchor.getSlrAnchorId());
				commentsTable = new SLRCommentsTable();
				secondLayout.addComponent(commentsTable.addCommentListComponent(object, slrCommentList, secondLayout));
				SLRCommentsUtil commentsUtil = new SLRCommentsUtil();
				secondLayout.addComponent(commentsUtil
						.getButtons(commentsTable, slrAnchor, slrDocId, secondLayout, "F"));
				slrCommentList = SlrCommentLocalServiceUtil
						.findByCommentTypAndAnchorId("R", slrAnchor.getSlrAnchorId());
				commentsTable = new SLRCommentsTable();
				thirdLayout.addComponent(commentsTable.addCommentListComponent(object, slrCommentList, thirdLayout));
				thirdLayout.addComponent(commentsUtil.getButtons(commentsTable, slrAnchor, slrDocId, thirdLayout, "R"));
			} catch (SystemException e) {
				_log.error(e.getMessage());
			}

			fourthLayout.addComponent(addDynComponent.getGraphsList(slrAnchor.getSlrAnchorId(), fourthLayout));
			fourthLayout.addComponent(new Label(""));
			tabSheet.addTab(firstLayout, Lang.getKey("slr.comment.details"), null);
			tabSheet.addTab(secondLayout, Lang.getKey("slr.comment.findings"), null);
			tabSheet.addTab(thirdLayout, Lang.getKey("slr.comment.recommendations"), null);
			tabSheet.addTab(fourthLayout, Lang.getKey("slr.comment.graph"), null);

		} else {
			secondLayout.addComponent(addDynComponent.addListComponent(object));
			tabSheet.addTab(firstLayout, Lang.getKey("slr.comment.details"), null);
			tabSheet.addTab(secondLayout, listName, null);
		}

		firstLayout.setHeight(100, TabSheet.UNITS_PERCENTAGE);
		secondLayout.setSpacing(true);
		secondLayout.setHeight(100, TabSheet.UNITS_PERCENTAGE);

		//addComponent(tabSheet);
		return tabSheet;
	}
    private static Log _log = LogFactoryUtil.getLog(SlrExplorerContainer.class);

}